/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System.Web;
using System.Web.UI;
using EPiServer.Core;

namespace EPiServer.Municipality.Web.UI.WebControls {
    /// <summary>
    /// Summary description for Path.
    /// </summary>
    /// <remarks>
    /// </remarks>
    [ToolboxData("<{0}:Path runat=server></{0}:Path>")]
    public class Path : Control {
        /* *******************************************************************
         *  Properties 
         * *******************************************************************/
        #region public string SeparatorText
        /// <summary>
        /// Get/Sets the SeparatorText of the Path
        /// </summary>
        /// <value></value>
        public string SeparatorText {
            get { return (string)(ViewState["SeparatorText"] ?? " / "); }
            set { ViewState["SeparatorText"] = value; }
        }
        #endregion
        #region public string StartPageTitle
        /// <summary>
        /// Get/Sets the StartPageTitle of the Path
        /// </summary>
        /// <value></value>
        public string StartPageTitle {
            get { return (string)(ViewState["StartPageTitle"] ?? SiteMap.RootNode.Title); }
            set { ViewState["StartPageTitle"] = value; }
        }
        #endregion
        #region public string LegendTranslationPath
        /// <summary>
        /// Get/Sets the LegendTranslationPath of the Path
        /// </summary>
        /// <value></value>
        public string LegendTranslationPath {
            get { return LanguageManager.Instance.Translate((string)ViewState["LegendTranslationPath"]); }
            set { ViewState["LegendTranslationPath"] = value; }
        }
        #endregion
        /* *******************************************************************
         *  Methods 
         * *******************************************************************/
        #region protected override void CreateChildControls()
        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls() {
            var path = PathUtil.GetCurrentPath(StartPageTitle, SeparatorText, false);

            if (path == null)
                return;

            Controls.AddAt(0, new LiteralControl(path));

            base.CreateChildControls();
        }
        #endregion
        #region protected override void Render(HtmlTextWriter writer)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer) {
            var current = SiteMap.CurrentNode as PageSiteMapNode;
            if (current != null) {
                if (PageReference.StartPage.CompareToIgnoreWorkID(current.CurrentPage.PageLink)) {
                    return;
                }
            }

            if (LegendTranslationPath != string.Empty) {
                writer.WriteBeginTag("strong");
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Write(LegendTranslationPath);
                writer.WriteEndTag("strong");
            }

            base.Render(writer);
        }
        #endregion
    }
}